<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 用户管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button
                    type="primary"
                    icon="el-icon-delete"
                    class="handle-del mr10"
                    @click="delAllSelection"
                >批量删除</el-button>
                <el-input v-model="queryParam.account" placeholder="账号" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <el-button type="primary" icon="el-icon-document-add" @click="handleAdd" style="float:right">新增</el-button>
            </div>
            <el-table
                :data="pageInfo.list"
                border
                class="table"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="account" label="账号"></el-table-column>
                <el-table-column prop="username" label="用户名"></el-table-column>
                 
                <el-table-column prop="tel" label="电话号码"></el-table-column>
                <el-table-column label="状态" align="center">
                    <template slot-scope="scope">
                        <el-tag>{{scope.row.status==='1'?'使用':'停用'}}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="memo" label="备注"></el-table-column>
                <el-table-column prop="email" label="邮箱"></el-table-column>
                <el-table-column prop="createdate" label="注册时间"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="pageInfo.pageNum"
                    :page-size="pageInfo.pageSize"
                    :total="pageInfo.total"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑用户" :close-on-click-modal="false" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="用户名">
                    <el-input v-model="form.username"></el-input>
                </el-form-item>
                <el-form-item label="密码">
                    <el-input v-model="form.password"></el-input>
                </el-form-item>
                <el-form-item label="电话号码">
                    <el-input v-model="form.tel"></el-input>
                </el-form-item>
                <el-form-item label="邮箱">
                    <el-input v-model="form.email"></el-input>
                </el-form-item>
                <el-form-item label="备注">
                    <el-input v-model="form.memo"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 新增弹出框 -->
        <el-dialog title="新增用户" :close-on-click-modal="false" :visible.sync="addVisible" width="30%">
            <el-form ref="addform" :model="addform" label-width="70px">
                <el-form-item label="账号">
                    <el-input v-model="addform.account"></el-input>
                </el-form-item>
                <el-form-item label="用户名">
                    <el-input v-model="addform.username"></el-input>
                </el-form-item>
                <el-form-item label="密码">
                    <el-input v-model="addform.password"></el-input>
                </el-form-item>
                <el-form-item label="电话号码">
                    <el-input v-model="addform.tel"></el-input>
                </el-form-item>
                <el-form-item label="邮箱">
                    <el-input v-model="addform.email"></el-input>
                </el-form-item>
                <el-form-item label="备注">
                    <el-input v-model="addform.memo"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveAdd">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { getUserList , editUser , deleteUser , addUser } from '@/api/User'

export default {
    name: 'basetable',
    data() {
        return {
            pageInfo: {
                pageNum: 1,
                pageSize: 10
            },
            queryParam: {
                account: ''
            },
            multipleSelection: [],
            delList: [],
            editVisible: false,
            addVisible: false,
            pageTotal: 0,
            form: {},
            addform:{},
            idx: -1,
            id: -1
        };
    },
    created() {
        this.getData();
    },
    methods: {
        // 获取数据
        getData() {
            getUserList(this.pageInfo.pageNum,this.queryParam.account).then(res=>{
                this.pageInfo = res; 
            })
        },
        // 触发搜索按钮
        handleSearch() {
            this.getData();
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            }).then(() => {
                deleteUser(row.id).then(res=>{
                    if( res.returnCode === '000000' ){
                        this.$message.success(`删除第 ${index + 1} 行成功`);
                        this.editVisible = false;
                        this.getData();
                    } else {
                        this.$message.error(`删除第 ${index + 1} 行失败`);
                    }    
                 })     
                }).catch(() => {
                    this.$message.error(`删除第 ${index + 1} 行失败`);
                });
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].account + ' ';
            }
            this.$message.error(`假装删除了${str}`);
            this.multipleSelection = [];
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.form = Object.assign({},row);   //避免模态框与表格直接联动
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
            editUser(this.form).then(res=>{
                if( res.returnCode === '000000' ){
                    this.$message.success(`修改第 ${this.idx + 1} 行成功`);
                    this.editVisible = false;
                    this.getData();
                } else {
                    this.$message.error(`修改第 ${this.idx + 1} 行失败`);
                }    
            })
        },
        // 分页导航
        handlePageChange(val) {
            this.pageInfo.pageNum = val;
            this.getData();
        },
        //新增操作
        handleAdd() {
            this.addform = {};
            this.addVisible = true;
        },
         //保存新增
        saveAdd() {
            addUser(this.addform).then(res=>{
                if( res.returnCode === '000000' ){
                    this.$message.success(`新增用户${this.addform.account}成功`);
                    this.addVisible = false;
                    this.getData();
                } else {
                    this.$message.error(`新增用户${this.addform.account}失败`);
                }    
            })
        }
        
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
